package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.IFacilitiesDao;
import com.cn.hotel.model.FacilitiesModel;
import com.cn.hotel.model.NavModel;
import com.cn.hotel.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FacilitiesImpl implements IFacilitiesDao {
    @Override
    public List<FacilitiesModel> findAll(FacilitiesModel facilitiesModel) {
        StringBuilder sql = new StringBuilder("select * from facilities where 1 = 1 ");
        if (facilitiesModel.getTitle()!=null&&!"".equals(facilitiesModel.getTitle())){
            sql.append(" and title like '%"+facilitiesModel.getTitle()+"%'");
        }
        if (facilitiesModel.getAuthor()!=null&&!"".equals(facilitiesModel.getAuthor())){
            sql.append(" and author like '%"+facilitiesModel.getAuthor()+"%'");
        }
        if (!"".equals(facilitiesModel.getStartReleaseTime())&&"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and release_time > " + facilitiesModel.getStartReleaseTime());
        }
        if ("".equals(facilitiesModel.getStartReleaseTime())&&!"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and release_time < " + facilitiesModel.getEndReleaseTime());
        }
        if (!"".equals(facilitiesModel.getStartReleaseTime())&&!"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and release_time between '"+facilitiesModel.getStartReleaseTime()+"' and '"+facilitiesModel.getEndReleaseTime()+"'");
        }
        sql.append(" order by id asc limit " + (facilitiesModel.getPageModel().getPageNum() - 1) * facilitiesModel.getPageModel().getPageSize() + "" +
                "," + facilitiesModel.getPageModel().getPageSize() + "");
        List<FacilitiesModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        try {
            while (resultSet.next()) {
                FacilitiesModel model = new FacilitiesModel();
                model.setId(resultSet.getInt("id"));
                model.setType(resultSet.getString("type"));
                model.setTitle(resultSet.getString("title"));
                model.setAuthor(resultSet.getString("author"));
                model.setContent(resultSet.getString("content"));
                model.setReleaseTime(resultSet.getString("release_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取数据库总条数
     * @param facilitiesModel
     * @return
     */
    @Override
    public int getNumber(FacilitiesModel facilitiesModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from facilities where 1 = 1 ");
        if (facilitiesModel.getTitle()!=null&&!"".equals(facilitiesModel.getTitle())){
            sql.append(" and title like '%"+facilitiesModel.getTitle()+"%'");
        }
        if (facilitiesModel.getAuthor()!=null&&!"".equals(facilitiesModel.getAuthor())){
            sql.append(" and author like '%"+facilitiesModel.getAuthor()+"%'");
        }
        if (!"".equals(facilitiesModel.getStartReleaseTime())&&"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and releaseTime > " + facilitiesModel.getStartReleaseTime());
        }
        if ("".equals(facilitiesModel.getStartReleaseTime())&&!"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and releaseTime < " + facilitiesModel.getEndReleaseTime());
        }
        if (!"".equals(facilitiesModel.getStartReleaseTime())&&!"".equals(facilitiesModel.getEndReleaseTime())){
            sql.append(" and releaseTime between '"+facilitiesModel.getStartReleaseTime()+"' and '"+facilitiesModel.getEndReleaseTime()+"'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try{
            resultSet.next();
            count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 新增设施介绍
     * @param facilitiesModel
     * @return
     */
    @Override
    public int add(FacilitiesModel facilitiesModel) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert into facilities(type,title,author,content,release_time) values('"+facilitiesModel.getType()+"'," +
                "'"+facilitiesModel.getTitle()+"','"+facilitiesModel.getAuthor()+"'" +
                ",'"+facilitiesModel.getContent()+"','"+facilitiesModel.getReleaseTime()+"')");
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 通过id查询信息 给页面赋值
     * @param temp
     * @return
     */
    @Override
    public FacilitiesModel findById(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0){
            sql.append("select * from facilities where id ="+temp);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        FacilitiesModel model = new FacilitiesModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setType(resultSet.getString("type"));
                model.setTitle(resultSet.getString("title"));
                model.setAuthor(resultSet.getString("author"));
                model.setContent(resultSet.getString("content"));
                model.setReleaseTime(resultSet.getString("release_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 保存编辑修改
     * @param facilitiesModel
     * @return
     */
    @Override
    public int edit(FacilitiesModel facilitiesModel) {
        StringBuilder sql = new StringBuilder("update facilities set");
        if (facilitiesModel.getType()!=null&&!"".equals(facilitiesModel.getType())){
            sql.append(" type = '"+facilitiesModel.getType()+"',");
        }
        if (facilitiesModel.getTitle()!=null&&!"".equals(facilitiesModel.getTitle())){
            sql.append(" title = '"+facilitiesModel.getTitle()+"',");
        }
        if (facilitiesModel.getAuthor()!=null&&!"".equals(facilitiesModel.getAuthor())){
            sql.append(" author = '"+facilitiesModel.getAuthor()+"',");
        }
        if (facilitiesModel.getContent()!=null&&!"".equals(facilitiesModel.getContent())){
            sql.append(" content = '"+facilitiesModel.getContent()+"',");
        }
        if (facilitiesModel.getReleaseTime()!=null&&!"".equals(facilitiesModel.getReleaseTime())){
            sql.append(" release_time = '"+facilitiesModel.getReleaseTime()+"',");
        }
        sql.append(" remark = '' where id = "+facilitiesModel.getId()+"");
        return JDBC.executeUpdate(sql.toString());
    }

    @Override
    public int delete(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0){
            sql.append("delete  from facilities where id="+temp);
        }
        return JDBC.executeUpdate(sql.toString());
    }
//-------------------------前端数据获取代码----------------------------------------
    @Override
    public List<FacilitiesModel> getFacilitiesModel() {
        StringBuilder sql = new StringBuilder("select * from facilities order by id desc limit 10");
        List<FacilitiesModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        try {
            while (resultSet.next()) {
                FacilitiesModel model = new FacilitiesModel();
                model.setType(resultSet.getString("type"));
                model.setTitle(resultSet.getString("title"));
                model.setReleaseTime(resultSet.getString("release_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
